home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1994 December / PSL Monthly Shareware CD-ROM (Public Software Library)(December 1994).bin / prgmming / win / pascal / strmsgs.pas < prev    next >
Pascal/Delphi Source File  |  1993-02-18  |  5KB  |  174 lines

  1. {
  2. Unit StrMsgs;
  3.  
  4. This unit is designed to display a messagebox containing both program-supplied
  5. and string resource text.  Foreign language translations are easier if the 
  6. text is contained in a string resource, but parts of a message (like file 
  7. names) need to be supplied at run time. Each message text string resource 
  8. can be split into 7 pieces, interspersed with 6 program-supplied strings.  
  9. Caption string resources can be split into 4, interspersed with 3 program-
  10. supplied strings. 
  11.  
  12. MsgTok was adapted from Function StrTok, written by John Cooper, Tower 
  13. Software & Systems, 76356,3601, uploaded to CIS as STRPAR.PAS and donated 
  14. to Public Domain. Thanks to John. 
  15.  
  16. To create MsgTok, StrTok was revisd to return #0 instead of nil when no 
  17. more tokens exist.  Thus if the return value of #0 is passed to a string 
  18. function like StrCopy, it won't cause the function to crash--which would 
  19. happen if nil was passed.  Likewise, MsgTok can be called again after it 
  20. has returned #0 without itself crashing on its own string function.  MsgTok 
  21. now uses a constant pChar delimeter, '+' for the exclusive purpose of 
  22. parsing the string table. 
  23.  
  24. GiveMsg reads the strings from a string resource using an offset of 
  25. IDS_Msg=15. (You could use any multiple of 16-1; I use 5007 since I have 
  26. already used IDs starting at 1,2,3 & 4 thousand) String resources are read 
  27. in blocks of 16 beginning with ID 0. IDS_Msg+1 is therefore the beginning 
  28. of a 16-string block.  Subsequent blocks begin at IDS_Msg+(n*16)+1. The 
  29. string resource must use a '+' to indicate where program supplied 
  30. information should be inserted. 
  31.  
  32. Example: 
  33.  
  34. Stringtable
  35. begin
  36.   IDS_Msg    , "English string table"
  37.   IDS_Msg + 1, "The quick brown + jumped over the lazy +."
  38.   IDS_Msg + 2, "+ is Quick"
  39. end
  40.  
  41. var
  42.   PcharOne, 
  43.   pCharTwo,
  44.   pCharThree: PChar;
  45.  
  46. begin
  47.    pCharOne:=strnew('fox');
  48.    pCharTwo:=strnew('dog');
  49.    pCharThree:=strnew('Jumping Fox');
  50.  
  51.    arMsg[1]:=pCharOne;
  52.    arMsg[2]:=pCharTwo;
  53.    arCap[1]:=pCharThree;
  54.    GiveMsg(hWindow,1,2,mb_OK or mb_iconexclamation,mb_iconexclamation);
  55.  
  56. Resulting Message box:
  57.  
  58.                        Jumping Fox is Quick
  59.  
  60.               The quick brown fox jumped over the lazy dog.
  61.  
  62. Note that in this case the caption begins with program supplied information, so
  63. the string resource must begin with a '+'.
  64.  
  65. This unit was written by:
  66.  
  67. Ravi Nielsen
  68. Maple Hill Software
  69. 73200,601
  70. and is donated to the Public Domain.
  71.  
  72. Please send your comments to BPascal forum or EMail.
  73. }
  74.  
  75. Unit StrMsgs;
  76.  
  77. Interface
  78.  
  79. uses wintypes,winprocs, Strings;
  80.  
  81. var
  82.   arMsg: array [1..6] of pChar;
  83.   arCap: array [1..3] of pChar;
  84.  
  85. Function MsgTok(Phrase:Pchar):Pchar;
  86. Function GiveMsg(hOwner:hWnd;TxtResID,CapResID,TextType,BeepType:Word):word;
  87.  
  88. Implementation
  89.  
  90. const
  91.   EOS: pchar = #0;
  92.   delimeter: pchar = '+';
  93.   IDS_Msg = 15;
  94.  
  95. var
  96.     TokenPointer,
  97.     WorkPointer : PChar;
  98.  
  99. Function MsgTok(Phrase:Pchar):Pchar;
  100.  var
  101.   NullPointer : Pchar;
  102.  begin
  103.   if (Phrase<>Nil) then WorkPointer := Phrase
  104.    else WorkPointer := TokenPointer;
  105.  
  106.   if workpointer<>EOS then
  107.     begin
  108.       NullPointer := StrPos(WorkPointer,Delimeter);
  109.       if (NullPointer<> Nil) then
  110.         begin
  111.           NullPointer^ := #0;
  112.           TokenPointer  := NullPointer + 1;
  113.         end
  114.        else
  115.           TokenPointer:=EOS;
  116.     end;
  117.   MsgTok := WorkPointer;
  118.  end;
  119.  
  120. procedure InitMsgArrays;
  121. var ndx:byte;
  122. begin
  123.   for ndx:=1 to 6 do arMsg[ndx]:=nil;
  124.   for ndx:=1 to 3 do arCap[ndx]:=nil;
  125. end;
  126.  
  127. Function GiveMsg(hOwner:hWnd;TxtResID,CapResID,TextType,BeepType:Word):Word;
  128. var
  129.   ndx:byte;
  130.   Msg,MsgRes,Cap,CapRes:array[0..255] of char;
  131.   BoxResponse:word;
  132. begin
  133.   MsgRes[0]:=#0;
  134.   CapRes[0]:=#0;
  135.   if TxtResID>0 then LoadString(hinstance,ids_Msg+TxtResID,MsgRes,256);
  136.   if CapResID>0 then LoadString(hinstance,ids_Msg+CapResID,CapRes,256);
  137.  
  138.   {Copy first message token to Msg}
  139.   StrCopy(Msg,MsgTok(MsgRes));
  140.   Ndx:=0;
  141.   {Intersperse program-supplied and resource-supplied message strings until out of
  142.    program-supplied strings or maximum of 6 in array is reached}
  143.   repeat
  144.     Inc(ndx);
  145.     if arMsg[ndx]<>nil then StrCat(Msg,arMsg[ndx]);
  146.     StrCat(Msg,MsgTok(nil));
  147.   until (arMsg[ndx]=nil) or (ndx=6);
  148.  
  149.   {Copy first caption token to Cap}
  150.   StrCopy(Cap,MsgTok(CapRes));
  151.   Ndx:=0;
  152.   {Intersperse program-supplied and resource-supplied caption strings until out of
  153.    program-supplied strings or maximum of 3 in array is reached}
  154.   repeat
  155.     Inc(ndx);
  156.     if arCap[ndx]<>nil then StrCat(Cap,arCap[ndx]);
  157.     StrCat(Cap,MsgTok(nil));
  158.   until (arCap[ndx]=nil) or (ndx=3);
  159.  
  160.   Messagebeep(BeepType);
  161. {$ifndef BWCC}
  162.   BoxResponse:=MessageBox(hOwner, Msg, Cap, TextType);
  163. {$else}
  164.   BoxResponse:=BWCCMessageBox(hOwner, Msg, Cap, TextType);
  165. {$endif}
  166.   InitMsgArrays;
  167.   GiveMsg:=BoxResponse;
  168. end;
  169.  
  170. begin
  171.   InitMsgArrays;
  172. end.
  173.  
  174.